<template>
  <div id="Login">
    <!-- 登录框主体部分 -->
    <div class="counter">
      <div class="title">
        <p class="welcome1">Welcome to KafuuChino RollCall</p>
      </div>
      <div class="body">
        <!-- 头像部分 -->
        <div class="user_header"></div>
        <!-- 表单部分 -->
        <div class="user_from">
          <el-form :model="LoginForm" ref="ruleForm" :rules="rules">
            <!-- 用户名部分 -->
            <el-form-item prop="username">
              <el-input v-model="LoginForm.username" placeholder="请输入用户名"></el-input>
            </el-form-item>
            <!-- 密码部分 -->
            <el-form-item prop="userpassword">
              <el-input type="password" show-password v-model="LoginForm.userpassword" placeholder="请输入密码"></el-input>
            </el-form-item>
            <!-- 提交按钮部分 -->
              <el-form-item>
                <el-button class="fr" type="primary" @click="login">登录</el-button>
              </el-form-item>
          </el-form>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  name:"Login",
  created() {
    this.check_login();
  },
  data() {
    return {
      LoginForm:{
        username:'',
        userpassword:''
      },
      rules:{
        username:[
          { required: true, message: '用户名不能为空', trigger: 'blur' },
        ],
        userpassword:[
          { required: true, message: '密码不能为空', trigger: 'blur' },
        ]
      }
    }
  },
  methods:{
    // 用户名 密码 登录
    login(){
      // 验证表单
      this.$refs["ruleForm"].validate(async (valid) => {
        if (valid) {
          // this.$message.success("非空验证成功！提交验证")
          let {data:res} = await this.$http.post("/login",this.LoginForm);
          if(res.status != 200) return this.$message.error(res.message);
          // 登录成功
          this.$message.success(res.message);
          // 存入Token
          window.localStorage.setItem("token",res.data.token);
          this.setUserinfo(res.data)
          // 跳转首页
          this.$router.push("/home")
        } else {
          this.$message.error("所有项必填！")
          return false;
        }
      });
    },
    // 检查登录
    async check_login(){
      // 检查浏览器是否已经存储Token
      let {data:res} = await this.$http.post("check_token");
      if(res.status == 200){
        this.setUserinfo(res.data)
        // 当前用户已登录 跳转Home
        this.$router.push("/home")
      }
    },
    // 赋值用户信息
    setUserinfo(data){
      let str = JSON.stringify(data)
      window.localStorage.setItem("userinfo",str);
    }
  }
}
</script>
<style lang="less" scoped>
#Login{
  width: 100%;
  height: 100vh;
  background-color: #DEE1E6;
  display: flex;
  align-items: center;
  justify-content: center;
}
.counter{
  margin: 0rem 1rem;
  width: 100%;
  max-width: 700px;
  // height: 25rem;
  background-color: #F1F3F4;
  border-radius: 0.625rem;
  box-shadow: 0rem 0px 1.25rem rgba(0, 0, 0, .1);
  padding: 1.5rem;
  .title{
    text-align: center;
    .welcome1{
      font-size: 1.5rem;
    }
    .welcome2{
      font-size: 1rem;
    }
  }
  .body{
    margin-top: 0.625rem;
    width: 100%;
    // background-color: rebeccapurple;
    .user_header{
      width: 100px;
      height: 100px;
      // background-color: #999;
      margin: 0 auto;
      background-image: url("https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=3644577905,2829957416&fm=26&gp=0.jpg");
      background-position: top center;
      background-size: 100%;
      border-radius: 0.3125rem;
      border: 5px solid rgba(0, 0, 0, .1);
    }
    .user_from{
      margin-top: 0.625rem;
    }
  }
}
</style>